package tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import tingshu.service.AlbumService;
import tingshu.util.Log;
import tingshu.view_object.BindView;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author houdaiqin
 * @date 2022/07/17 16:27
 **/
@WebServlet("/studio/album/bind.json")
public class BindJsonServlet extends HttpServlet {
    private final AlbumService albumService=new AlbumService();
    private final ObjectMapper objectMapper=new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserVO currentUser;
        HttpSession session=req.getSession(false);
        if (session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");
        }else{
            currentUser=null;
        }
        if (currentUser==null){
            Log.println("用户未登录");
            return;
        }
        int aid=Integer.parseInt(req.getParameter("aid"));
        BindView view=albumService.getBindInfo(currentUser,aid);
        String json=objectMapper.writeValueAsString(view);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer= resp.getWriter();
        writer.println(json);
    }
}